package com.kust.eduService.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kust.commomUtils.Result;
import com.kust.eduService.entity.Teacher;
import com.kust.eduService.entity.vo.TeacherQuery;
import com.kust.eduService.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-06-14
 */
@Api(tags = "讲师管理")
@RestController
@RequestMapping("/eduService/edu-teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    //http:localhost:8080/eduService/edu-teacher/findAll
    @GetMapping("/findAll")
    public Result findAllTeacher(){
        List<Teacher> list = teacherService.list(null);
        return Result.ok().data("items",list).message("查询成功");
    }

    //逻辑删除
    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("/{id}")
    public Result deleteTeacher(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable("id") String id){
        if(teacherService.removeById(id)){
            return Result.ok().message("删除成功");
        }else
            return Result.error().message("删除失败");
    }

    //3、分页查询讲师的方法
    //current 当前页
    //limit 每页记录数
    @ApiOperation(value = "分页讲师列表")
    @GetMapping("/pageTeacher/{current}/{limit}")
    public Result pageTeacher(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable("current") Long current,
                              @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit") Long limit){
        //创建Page对象
        //调用方法时，底层封装，
        Page<Teacher> eduTeacherPage= new Page<>(current,limit);
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc();
        teacherService.page(eduTeacherPage,queryWrapper);
        long total=eduTeacherPage.getTotal(); //记录总页数
        List<Teacher> records = eduTeacherPage.getRecords();//数据list集合
        return Result.ok().data("total",total).data("rows",records);

    }



    //4、条件查询带分页
    @ApiOperation(value = "分页讲师列表条件查询")
    @PostMapping("/pageTeacherCondition/{current}/{limit}")
    public Result pageTeacherCondition(@ApiParam(name = "current", value = "当前页码", required = true) @PathVariable("current") Long current,
                                       @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable("limit") Long limit,
                                       @RequestBody(required = false) TeacherQuery teacherQuery){
        //创建page对象
        Page<Teacher> eduTeacherPage= new Page<>(current,limit);
        IPage<Teacher> pageQuery = teacherService.pageQuery(eduTeacherPage, teacherQuery);

        //调用方法实现条件查询分页
        long total = pageQuery.getTotal();//总记录数
        List<Teacher> records = pageQuery.getRecords(); //数据list集合
        return Result.ok().data("total",total).data("rows",records);
    }

    //添加讲师
    @ApiOperation(value = "添加讲师")
    @PostMapping("/addTeacher")
    public Result addTeacher(@ApiParam("json的teacher对象") @RequestBody Teacher teacher){
        System.out.println("添加讲师=====》》》"+teacher );
        boolean save = teacherService.save(teacher);
        if(save){
            return Result.ok().message("添加成功");
        }else {
            return Result.error().message("添加失败");
        }
    }

    //根据id进行查询
    @ApiOperation("根据id进行查询")
    @GetMapping("getTeacher/{id}")
    public Result getTeacher(@ApiParam("查询讲师的id") @PathVariable("id") String id){
        Teacher teacherById = teacherService.getById(id);
        return Result.ok().data("teacher",teacherById);
    }

    //修改讲师
    @ApiOperation(value = "修改讲师信息")
    @PostMapping("/updateTeacher")
    public Result updateTeacher(@RequestBody Teacher teacher){
        System.out.println(teacher);
        boolean flag = teacherService.updateById(teacher);
        if(flag){
            return Result.ok().message("修改成功");
        }else {
            return Result.error().message("修改失败");
        }
    }
}
